package com.zx.oa.controller;

import com.alibaba.excel.EasyExcel;
import com.zx.oa.entity.MarketClass;
import com.zx.oa.entity.MarketStudent;
import com.zx.oa.entity.ShowMarketStudent;
import com.zx.oa.service.marketTypeClass.MarketTypeClassService;
import com.zx.oa.service.marketTypeClass.MarketTypeClassServiceImpl;
import com.zx.oa.service.marketStudent.MarketStudentService;
import com.zx.oa.service.marketStudent.MarketStudentServiceImpl;
import com.zx.oa.utils.PageSupport;
import com.zx.oa.utils.excel.MarketStudentExcel;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/marketTypeStudentServlet.do")
public class MarketTypeStudent extends HttpServlet {

    MarketStudentService marketStudentService=new MarketStudentServiceImpl();
    MarketTypeClassService marketTypeClassService=new MarketTypeClassServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String operation=request.getParameter("param");
        if ("cha".equals(operation)){
            chaServlet(request,response);
        }else if("excel".equals(operation)){
            excelStudentServlet(request,response);
        }else if ("add_cha".equals(operation)){
            add_chaServlet(request,response);
        } else if ("add".equals(operation)) {
            addServlet(request,response);
        }else if ("del".equals(operation)){
            delServlet(request,response);
        }else if("modify_cha".equals(operation)){
            modify_chaServlet(request,response);
        } else if ("modify".equals(operation)) {
            modifyServlet(request,response);
        }else if("select".equals(operation)){
            selectServlet(request,response);
        }
    }

    private void selectServlet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id= Integer.valueOf(request.getParameter("id"));
        //通过id获取学生信息
        MarketStudent marketStudent=marketStudentService.getStudentById(id);
        String className=request.getParameter("className");
        //通过班级名称获取班级信息
        MarketClass marketCla=marketTypeClassService.getMarketClassInfoByClassName(className);
        //获取所有班级信息
        List<MarketClass> marketClass= marketTypeClassService.getMarketClassInfo();
        request.setAttribute("className",className);
        request.setAttribute("marketClass",marketClass);
        request.setAttribute("marketStudentInfo",marketStudent);
        request.setAttribute("marketCla",marketCla);

        Integer pageIndex= Integer.valueOf(request.getParameter("pageIndex"));
        String namec=request.getParameter("namec");
        String classname=request.getParameter("classname");
        String stuType=request.getParameter("stuType");
        String intention=request.getParameter("intention");
        request.setAttribute("pageIndex",pageIndex);
        request.setAttribute("namec",namec);
        request.setAttribute("classname",classname);
        request.setAttribute("stuType",stuType);
        request.setAttribute("intention",intention);
        request.getRequestDispatcher("jsp/selectMarketStudent.jsp").forward(request,response);
    }

    private void modifyServlet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id= Integer.valueOf(request.getParameter("id"));
        String name=request.getParameter("name");
        Integer classId= Integer.valueOf(request.getParameter("classId"));
        String fromSchool=request.getParameter("fromschool");
        String education=request.getParameter("education");
        String phone= request.getParameter("phone");
        String QQ= request.getParameter("QQ");
        String character=request.getParameter("character");
        String remarks=request.getParameter("remarks");
        String Qintention=request.getParameter("intention_cha");
        String createName= (String) request.getSession().getAttribute("loginName");
        MarketStudent marketStudent=new MarketStudent(id,name,classId,fromSchool,education,phone,QQ,character,remarks,Qintention);
        boolean flag=marketStudentService.modifyMarketStudent(marketStudent,createName);
        if (flag){
            Integer pageIndex= Integer.valueOf(request.getParameter("pageIndex"));
            String namec=request.getParameter("namec");
            String classname=request.getParameter("classname");
            String stuType=request.getParameter("stuType");
            String intention=request.getParameter("intention");
            request.setAttribute("pageIndex",pageIndex);
            request.setAttribute("namec",namec);
            request.setAttribute("classname",classname);
            request.setAttribute("stuType",stuType);
            request.setAttribute("intention",intention);
            request.getRequestDispatcher("marketTypeStudentServlet.do?param=cha").forward(request,response);
        }
    }

    private void modify_chaServlet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取所有市场类型
        List<String> typeList=marketTypeClassService.getClassTypeAll();

        //获取所有市场班级信息
        List<MarketClass> marketClassList=marketTypeClassService.getMarketClassInfo();

        //获取所有性格特点
        List<String> characterList=marketStudentService.getCharacter();

        //获取所有培训意向
        List<String> intentionList=marketStudentService.getIntentionAll();

        Integer id= Integer.valueOf(request.getParameter("id"));

        MarketStudent marketStudent=marketStudentService.getStudentById(id);

        request.setAttribute("typeList",typeList);
        request.setAttribute("marketClassList",marketClassList);
        request.setAttribute("characterList",characterList);
        request.setAttribute("intentionList",intentionList);

        request.setAttribute("marketStudentInfo",marketStudent);

        Integer pageIndex= Integer.valueOf(request.getParameter("pageIndex"));
        String namec=request.getParameter("namec");
        String classname=request.getParameter("classname");
        String stuType=request.getParameter("stuType");
        String intention=request.getParameter("intention");

        request.setAttribute("pageIndex",pageIndex);
        request.setAttribute("namec",namec);
        request.setAttribute("classname",classname);
        request.setAttribute("stuType",stuType);
        request.setAttribute("intention",intention);

        request.getRequestDispatcher("jsp/modifyMarketStudent.jsp").forward(request,response);
    }

    private void delServlet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        Integer id= Integer.valueOf(request.getParameter("id"));
        boolean flag=marketStudentService.delMarketStudentById(id);
        if (flag){
            Integer pageIndex= Integer.valueOf(request.getParameter("pageIndex"));
            String namec=request.getParameter("namec");
            String classname=request.getParameter("classname");
            String stuType=request.getParameter("stuType");
            String intention=request.getParameter("intention");
            request.setAttribute("pageIndex",pageIndex);
            request.setAttribute("namec",namec);
            request.setAttribute("classname",classname);
            request.setAttribute("stuType",stuType);
            request.setAttribute("intention",intention);
            request.getRequestDispatcher("marketTypeStudentServlet.do?param=cha").forward(request,response);
        }
    }

    private void addServlet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name=request.getParameter("name");
        String className=request.getParameter("classname");
        String fromSchool=request.getParameter("fromschool");
        String education=request.getParameter("education");
        Integer phone= Integer.valueOf(request.getParameter("phone"));
        Integer QQ= Integer.valueOf(request.getParameter("QQ"));
        String character=request.getParameter("character");
        String remarks=request.getParameter("remarks");
        String intention=request.getParameter("intention");
        String createName= (String) request.getSession().getAttribute("loginName");
        boolean flag=marketStudentService.addMarketStudent(null,name,className,fromSchool,education,phone,QQ,character,remarks,intention,null,createName);
        if (flag){
            response.sendRedirect("marketTypeStudentServlet.do?param=cha");
        }
    }

    private void add_chaServlet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取所有市场类型
        List<String> typeList=marketTypeClassService.getClassTypeAll();

        //获取所有市场班级信息
        List<MarketClass> marketClassList=marketTypeClassService.getMarketClassInfo();

        //获取所有性格特点
        List<String> characterList=marketStudentService.getCharacter();

        //获取所有培训意向
        List<String> intentionList=marketStudentService.getIntentionAll();

        request.setAttribute("typeList",typeList);
        request.setAttribute("marketClassList",marketClassList);
        request.setAttribute("characterList",characterList);
        request.setAttribute("intentionList",intentionList);

        request.getRequestDispatcher("jsp/addMarketStudent.jsp").forward(request,response);
    }

    private void excelStudentServlet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name=null;
        String className=null;
        String stuType=null;
        String intention=null;
        String queryName=request.getParameter("namec");
        String queryClass=request.getParameter("classname");
        String queryStuType=request.getParameter("stuType");
        String queryIntention=request.getParameter("intention");
        if(null!=queryName&&!"".equals(queryName)){
            name=queryName;
        }
        if(null!=queryClass&&!"".equals(queryClass)){
            className=queryClass;
        }
        if(null!=queryStuType&&!"".equals(queryStuType)){
            stuType=queryStuType;
        }
        if(null!=queryIntention&&!"".equals(queryIntention)){
            intention=queryIntention;
        }

        SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取到时间，以时间来定义下载Excel的名称
        String downLoadTime=sf.format(new Date());

        List<ShowMarketStudent> showMarketStudents=marketStudentService.getStudentAll(name,className,stuType,intention);
        //设置Excel响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //下载的Excel文件名称
        String fileName="showMarketStudent"+downLoadTime+".xlsx";
        response.setHeader("Content-disposition","allachment;filename="+fileName);
        EasyExcel.write(response.getOutputStream(),
                MarketStudentExcel.class).sheet("市场类学生信息").doWrite(showMarketStudents);
    }

    private void chaServlet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name=null;
        String className=null;
        String stuType=null;
        String intention=null;
        String queryName=request.getParameter("namec");
        String queryClass=request.getParameter("classname");
        String queryStuType=request.getParameter("stuType");
        String queryIntention=request.getParameter("intention");
        if(null!=queryName&&!"".equals(queryName)){
            name=queryName;
        }
        if(null!=queryClass&&!"".equals(queryClass)){
            className=queryClass;
        }
        if(null!=queryStuType&&!"".equals(queryStuType)){
            stuType=queryStuType;
        }
        if(null!=queryIntention&&!"".equals(queryIntention)){
            intention=queryIntention;
        }
        String pageIndex= request.getParameter("pageIndex");
        if(pageIndex==null){
            //第一次访问查询的servlet的时候没有pageIndex，赋默认值1
            pageIndex="1";
        }
        int currentPageNo=1;//当前页号
        int pageSize= PageSupport.pageSizeVal;//页面尺寸复制
        currentPageNo=Integer.parseInt(pageIndex);

        //获取学生信息
        List<ShowMarketStudent> showMarketStudentList=marketStudentService.getStudentAll(name,className,stuType,intention,currentPageNo,pageSize);

        //获取到总记录数
        int totalCount= marketStudentService.getStudentTotalCount(name,className,stuType,intention);

        //获取所有培训意向
        List<String> intentionList=marketStudentService.getIntentionAll();

        //获取所有市场类型
        List<String> typeList=marketTypeClassService.getClassTypeAll();

        //获取所有市场班级信息
        List<MarketClass> marketClassList=marketTypeClassService.getMarketClassInfo();

        //存储页面信息
        PageSupport pages=new PageSupport();
        //设置页面尺寸
        pages.setPageSize(pageSize);
        //设置页面总记录数（注意此方法计算得到总页面数量）
        pages.setTotalCount(totalCount);
        //获取到总的页数
        int totalPageCount=pages.getTotalPageCount();
        if(currentPageNo<1){
            currentPageNo=1;
        }else if(currentPageNo>totalPageCount){
            currentPageNo=totalPageCount;
        }
        //将当前页号存储到pages对象
        pages.setCurrentPageNo(currentPageNo);

        //获取所有学生信息
        List<ShowMarketStudent> MarketStudentList=marketStudentService.getStudentAll(name,className,stuType,intention);

        request.setAttribute("pageInfo",pages);//分页信息
        request.setAttribute("showMarketStudentList",showMarketStudentList);//分页学生信息
        request.setAttribute("intentionList",intentionList);//意向信息
        request.setAttribute("typeList",typeList);//班级类型信息
        request.setAttribute("marketClassList",marketClassList);//班级信息
        request.setAttribute("MarketStudentList",MarketStudentList);//全部学生信息
        request.setAttribute("name",name);//名字回显

        request.getRequestDispatcher("jsp/showMarketTypeStudent.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
